package systems;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import debugs.ArticleDebug;

import helpers.CacheHelper;
import entities.Article;
import entities.Category;

public class Recently {
	
	private static String cachePrefix = "recently_";
	
	public static void insertArticle(String id,  String type, Article article) {
		int limit = 20;
		
		String cacheName = cachePrefix + type +"_" +  id;
		
		List<Article> articles = (List<Article>) CacheHelper.get(cacheName);
		
		if (articles == null) {
			articles = new ArrayList<Article>();
		}
		
		if (article != null) {
		
			for (int i = 0; i < articles.size(); i++) {
				if (articles.get(i) != null && articles.get(i).getId().equals(article.getId())) {
					articles.remove(articles.get(i));
				}
			}
			
			if (articles.size() >= limit) {
				articles.remove(limit - 1);
			}
			
			articles.add(0, article);
			CacheHelper.put(cacheName, articles);
		}
	}
	
	public static void removeArticle(String id, String type, Article article) {
		
		String cacheName = cachePrefix + type +"_" +  id;
		
		List<Article> articles = (List<Article>) CacheHelper.get(cacheName);
		
		if (articles == null) {
			articles = new ArrayList<Article>();
		}
		
		for (int i = 0; i < articles.size(); i++) {
			if (articles.get(i).getId().equals(article.getId())) {
				articles.remove(articles.get(i));
			}
		}
		
		CacheHelper.put(cacheName, articles);
	}
	
	public static List<Article> getArticles(String id, String type) {
		String cacheName = cachePrefix + type +"_" +  id;
		
		List<Article> articles = (List<Article>) CacheHelper.get(cacheName);
		
		if (articles == null) {
			articles = new ArrayList<Article>();
		}
		
		return articles;
	}
	
	public static void update(Article article) {
		ArticleDebug.debug(article);
		//Insert recently
		Recently.insertArticle("", "recently_article", article);
		
		//Update lastest
		Recently.insertArticle("", "lastest_article", article);
		
		if (!article.getCategory().isEmpty()) {
			Recently.insertArticle(article.getCategory(), "category", article);
		}
		
		if (article.getKeywords().size() > 0) {
			for (String tag : article.getKeywords()) {
				Recently.insertArticle(tag, "tag", article);
			}
		}
		
		if (!article.getCreator().isEmpty()) {
			Recently.insertArticle(article.getCreator(), "author", article);
		}
		
		Calendar cal = Calendar.getInstance();
		cal.setTime(article.getCreated());
		
		int month = cal.get(Calendar.MONTH) + 1;
		String name = (month < 10 ? "0" + month : month) + "-" + cal.get(Calendar.YEAR);
		
		Recently.insertArticle(name, "archives", article);
		
	}
	
	public static void remove(Article article) {
		//Remove recently
		Recently.removeArticle("", "recently_article", article);
		
		//Remove lastest cache
		Recently.removeArticle("", "lastest_article", article);
		
		if (!article.getCategory().isEmpty()) {
			System.out.println("remove category " + article.getCategory());
			Recently.removeArticle(article.getCategory(), "category", article);
		}
		
		if (article.getKeywords().size() > 0) {
			for (String tag : article.getKeywords()) {
				Recently.removeArticle(tag, "tag", article);
			}
		}
		
		if (!article.getCreator().isEmpty()) {
			System.out.println("remove author " + article.getCreator());
			Recently.removeArticle(article.getCreator(), "author", article);
		}
		
		Calendar cal = Calendar.getInstance();
		cal.setTime(article.getCreated());
		
		int month = cal.get(Calendar.MONTH) + 1;
		String name = (month < 10 ? "0" + month : month) + "-" + cal.get(Calendar.YEAR);
		System.out.println("remove archives " + name);
		Recently.removeArticle(name, "archives", article);
		
	}
	
	public static void updateView(Article article) {
		//Insert recently
		Recently.insertArticle("", "recently_article", article);
	}
	
	public static void removeView(Article article) {
		//Remove recently
		Recently.removeArticle("", "recently_article", article);
	}
}
